#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sqlite3.h>

#include "get_data.h"
#include "pack.h"
#include "logger.h"

/*获取设备名称*/
int get_device_name(char *device_name, int device_len, int device_number)
{
	int							ret;

	if(!device_name||device_len<DEVICE_LEN)
	{
		log_warn("Invalid input arugment\n");
		return -1;
	}

	ret=snprintf(device_name, device_len, "RPI%04d", device_number);
	if(ret<0 || ret>=device_len)
	{
		return -2;
	}
	return 0;
}
/*获取时间*/
int get_current_time(char *currentTimeStr)
{
	time_t						currentTime;
	struct tm					*localTime;

	time(&currentTime);
	localTime = localtime(&currentTime);
	snprintf(currentTimeStr, MSG_SIZE, "%d/%d/%d——%d:%d:%d", localTime->tm_year+1900,localTime->tm_mon+1,localTime->tm_mday,localTime->tm_hour,localTime->tm_min,localTime->tm_sec);
	return 0;
}

/*获取温度*/
int get_temperature(float *temp)
{
	int						fd				=	-1;
	int						found			=	-1;
	char					buf[MSG_SIZE];
	char					chip_sn[MSG_SIZE];
	char					w1_path[MSG_SIZE]	="/w2/";
	char 					*ptr 			=	NULL;
	DIR 					*dirp			=	NULL;
	struct dirent			*direntp		=	NULL;

	
	if((dirp = opendir(w1_path)) == NULL)
    {
		log_warn("opendir error: %s\n", strerror(errno));
		close(fd);
		closedir(dirp);
        return -3;
    }	
	while(NULL!=(direntp=readdir(dirp)))
	{
		if(strstr(direntp->d_name,"28-"))
		{
			log_info("d_name:%s\n",direntp->d_name);
			strncpy(chip_sn,direntp->d_name,sizeof(chip_sn));
			found=1;
			log_info("chip_sn:%s\n",chip_sn);
		}
	}
	closedir(dirp);
	if(!found)
	{
		log_warn("in get_temperature:can not find '28-'");
		return -4;
	}
	strncat(w1_path,chip_sn,sizeof(w1_path)-strlen(w1_path));
	log_info("w1_path:%s\n",w1_path);
	strncat(w1_path,"/w1_slave",sizeof(w1_path)-strlen(w1_path));
	log_info("w1_path:%s\n",w1_path);
	fd=open(w1_path,O_RDONLY);
	if(!fd)
	{
		log_warn("in get_temperature:cant not find w1_path:%s\n",strerror(errno));
		return -1;
	}
	memset(buf,0,sizeof(buf));
	read(fd,buf,sizeof(buf));
	log_info("in get_temperature:buf: %s\n",buf);
	ptr=strstr(buf,"t=");
	if(!ptr)
	{
		log_warn("can not find 't='\n");
		return -2;
	}
	log_info("ptr:%s\n",ptr);
	ptr=ptr+2;
	log_info("ptr+2:%s\n",ptr);
	*temp=atof(ptr);
	log_info("temp:%f\n",*temp/1000);
	close(fd);
	return 0;
}

/*
int get_device_name(char *device_name)
{
	int						fd				=	-1;
	int						found			=	-1;
	char					buf[MSG_SIZE];
	char					chip_sn[MSG_SIZE];
	char					w1_path[MSG_SIZE]	="/w2/";
	char					*ptr			=	NULL;
	DIR						*dirp			=	NULL;
	struct dirent			*direntp		=	NULL;

	if((dirp = opendir(w1_path)) == NULL)
    {
        printf("opendir error: %s\n", strerror(errno));
		close(fd);
		closedir(dirp);
        return -2;
	}
	while(NULL!=(direntp=readdir(dirp)))
	{
		if(strstr(direntp->d_name,"28-"))
		{
			dbg_printf("d_name:%s\n",direntp->d_name);
			strncpy(chip_sn,direntp->d_name,sizeof(chip_sn));
			found=1;
			dbg_printf("chip_sn:%s\n",chip_sn);

			strcpy(device_name,chip_sn);
			dbg_printf("device name:%s\n",device_name);
		}
	}
	if(!found)
	{
		log_warn("can not find '28-'");
		closedir(dirp);
		return -4;
	}
	closedir(dirp);
	close(fd);
	return 0;
}
*/
